<?php

namespace app\erp\model;

use think\Model;
use app\erp\validate\Jdrule;

class Jddb extends Model {
	protected $pk = 'jd_id';
	protected $name = 'jd';
	protected $autoWriteTimestamp = true;
	public $param = array ();
	protected $insert = [ 
			'site_id' => SITE_ID 
	];
	public function base($query) {
		$query->where ( 'site_id', SITE_ID );
		if (isset ( $this->param ['jd_id'] ) && $this->param ['jd_id']) {
			$query->jdId ( $this->param ['jd_id'] );
		}
		if (isset ( $this->param ['goods_name'] ) && $this->param ['goods_name']) {
			$query->goodsName ( $this->param ['goods_name'] );
		}
		if (isset ( $this->param ['goods_sku'] ) && $this->param ['goods_sku']) {
			$query->goodsSku ( $this->param ['goods_sku'] );
		}
		if (isset ( $this->param ['spu'] ) && $this->param ['spu']) {
			$query->Spu ( $this->param ['spu'] );
		}
		if (isset ( $this->param ['goods_model'] ) && $this->param ['goods_model']) {
			$query->goodsModel ( $this->param ['goods_model'] );
		}
		if (isset ( $this->param ['goods_brand'] ) && $this->param ['goods_brand']) {
			$query->goodsBrand ( $this->param ['goods_brand'] );
		}
	}
	public function scopeGoodsName($query, $goods_name) {
		$query->where ( 'goods_name', 'like', '%' . $goods_name . '%' );
	}
	public function scopeGoodsSku($query, $goods_sku) {
		$query->where ( 'goods_sku', 'like', '%' . $goods_sku . '%' );
	}
	public function scopeGoodsModel($query, $goods_model) {
		$query->where ( 'goods_model', 'like', '%' . $goods_model . '%' );
	}
	public function scopeGoodsBrand($query, $goods_brand) {
		$query->where ( 'goods_brand', 'like', '%' . $goods_brand . '%' );
	}
	public function scopeSpu($query, $spu) {
		$query->where ( 'spu', 'like', '%' . $spu . '%' );
	}
	public function scopeJdId($query, $jd_id) {
		$query->where ( 'jd_id', $jd_id );
	}
	public function ajaxSave() {
		$rule = new Jdrule ();
		if (isset ( $this->param ['jd_id'] ) && $this->param ['jd_id']) {
			$this->save ( $this->param, [ 
					'jd_id' => $this->param ['jd_id'] 
			] );
		} else {
			$this->param ['site_id'] = SITE_ID;
			if (! $rule->check ( $this->param )) {
				return $rule->getError ();
			}
			$this->allowField ( true )->save ( $this->param );
			return 'success';
		}
	}
	public function ajaxSelect() {
		$return ['data'] = $this->ajaxList ();
		$return ['count'] = $this->ajaxCount ();
		return $return;
	}
	public function ajaxList() {
		$data = $this->order ( 'jd_id desc' )->page ( $this->param ['page'], $this->param ['limit'] )->select ();
		if (! $data->isEmpty ()) {
			$data = $data->toArray ();
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->handleData ( $val );
			}
		}
		return $data;
	}
	public function ajaxCount() {
		$count = $this->count ();
		return $count;
	}
	public function ajaxDelete($goods_sku) {
		$this->where ( 'goods_sku', $goods_sku )->delete ();
		return 'success';
	}
	// 数据处理
	protected function handleData($val) {
		foreach ( $val as $k => $v ) {
			$val [$k] = $v;
			switch ($k) {
				default :
					break;
			}
		}
		return $val;
	}
}